package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.service.ShippingLineService;
import com.sz.biz.logistics.base.dto.ShippingLineDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: ShippingLineController <br>
 * Author: wentao.chang <br>
 * Date: 2017-08-04 15:19:00
 */
@RestController
@RequestMapping("${api}/biz/shippinglines")
@Api(description = " ", tags = "026-02、海运航线")
public class ShippingLineController extends BaseController {
    @Autowired
    private ShippingLineService shippingLineService;


    @ApiOperation(value = "添加海运航线", notes = "添加海运航线, <br>权限:" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    @RequestMapping(value = "/", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    public ResultDto createShipModel(@RequestBody @ApiParam(value = "海运航线", required = true) ShippingLineDto model) {
        Integer id = shippingLineService.save(model);
        return this.getSaveSuccessDto(MessagesCodes.MODULE_SHIPPINGLINE);
    }

    @ApiOperation(value = "修改海运航线", notes = "修改海运航线<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "海运航线Id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "model", value = "海运航线", required = true, dataType = "ShippingLineDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    public ResultDto updateShipModel(@PathVariable("id") int id, @RequestBody ShippingLineDto model) {
        shippingLineService.update(id, model);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_SHIPPINGLINE);
    }

    @ApiOperation(value = "批量删除海运航线", notes = "批量删除海运航线<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    public ResultDto deleteShipModels(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        shippingLineService.deleteByIds(ids);
        return this.getDeleteSuccessDto(MessagesCodes.MODULE_SHIPPINGLINE);
    }

    @ApiOperation(value = "获取海运航线", notes = "获取海运航线<br>权限：")
    @ApiImplicitParam(name = "id", value = "海运航线Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getShipModel(@PathVariable("id") int id) {
        ShippingLineDto dto = shippingLineService.findById(id);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "获取海运航线", notes = "获取海运航线<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "countryCodeFrom", value = "起运国家", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "codeStart", value = "起运港口英文名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "countryCodeTo", value = "目的国家", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "codeEnd", value = "目的港口英文名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "carrierId", value = "承运人id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public ResultDto getShipModels(@RequestParam(required = false) String q,
                                   @RequestParam(required = false) String countryCodeFrom,
                                   @RequestParam(required = false) String codeStart,
                                   @RequestParam(required = false) String countryCodeTo,
                                   @RequestParam(required = false) String codeEnd,
                                   @RequestParam(required = false) Integer carrierId,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "20") int pageSize,
                                   @RequestParam(required = false,defaultValue = "code") String sort,
                                   @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return shippingLineService.findShippingLineListPage(new QueryParams(pageSize, pageIndex, q, sort, asc)
                .put("carrierId", carrierId)
                .put("countryCodeFrom",countryCodeFrom)
                .put("codeStart",codeStart)
                .put("countryCodeTo",countryCodeTo)
                .put("codeEnd",codeEnd));
    }

    @ApiOperation(value = "获取海运航线短接口", notes = "获取海运航线短接口<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/short", method = {RequestMethod.GET})
    public ResultDto getShipModels(@RequestParam(required = false) String q,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        return shippingLineService.findShippingLineShortListPage(new QueryParams(pageSize, pageIndex, q, null, true));
    }

    @ApiOperation(value = "检查海运航线名是否存在", notes = "检查海运航线名是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkname", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    public ResultDto checkNameExist(@RequestParam(required = false) String name,
                                    @RequestParam(required = false) Integer id) {
        shippingLineService.checkName(name, id);
        return new ResultDto();
    }
    @ApiOperation(value = "检查海运航线编码是否存在", notes = "检查海运航线编码是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING)
    public ResultDto checkCodeExist(@RequestParam(required = false) String code,
                                    @RequestParam(required = false) Integer id) {
        shippingLineService.checkCode(code, id);
        return new ResultDto();
    }
}
